package Tree;

/**
 * @Classname JZ68二叉搜索树的最近公共祖先II
 * @Description TODO
 * @Date 2023/2/23 21:45
 * @Created by xjl
 */
public class JZ68二叉搜索树的最近公共祖先II {

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int value=root.val;
        if (p.val<value && q.val<value){
            return lowestCommonAncestor(root.left,p,q);
        }
        if (p.val>value&&q.val>value){
            return lowestCommonAncestor(root.right,p,q);
        }
        if (root==q||root==q){
            return root;
        }
        return root;
    }
}
